package com.browan.background;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

public class StoreManager
{
	public static String PARENT_DIR = Environment.getExternalStorageDirectory() + "/Pictures/background/";

	private static final String TAG = StoreManager.class.getSimpleName();

	public static Bitmap getBitmap(String url)
	{
		String name = Md5Tool.getMd5Value(url) + ".jpg";
		Bitmap bitmap = readBitmap(name);

		if (null == bitmap)
		{
			bitmap = downBitmap(url);

			if (null != bitmap)
			{
				storeBitmap(name, bitmap);
			}
		}
		return bitmap;
	}

	public static boolean exist(String name)
	{
		File file = new File(PARENT_DIR + name);
		if(null == file)return false;
		return file.exists();
	}

	public static void storeBitmap(String name, Bitmap bitmap)
	{
		Log.e(TAG, "storeBitmap");

		FileOutputStream foStream = null;

		File parent = new File(PARENT_DIR);

		if (!parent.exists())
		{
			if (!parent.mkdirs())
			{
				return;
			}
		}
		name = PARENT_DIR + name;
		try
		{
			foStream = new FileOutputStream(name);
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, foStream);
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (null != foStream)
			{
				try
				{
					foStream.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				foStream = null;
			}
		}
	}

	public static Bitmap readBitmap(String name)
	{

		File parent = new File(PARENT_DIR);

		if (!parent.exists())
		{
			return null;
		}
		FileInputStream fileInputStream = null;
		Bitmap bitmap = null;
		try
		{
			fileInputStream = new FileInputStream(PARENT_DIR + name);
			bitmap = BitmapFactory.decodeStream(fileInputStream);
		}
		catch (FileNotFoundException e)
		{
			bitmap = null;
		}
		finally
		{
			if (null != fileInputStream)
			{

				try
				{
					fileInputStream.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				fileInputStream = null;
			}
		}
		return bitmap;
	}

	public static Bitmap downBitmap(String urlStr)
	{

		Log.e(TAG, "downBitmap");

		Bitmap bitmap = null;
		try
		{
			HttpGet httpRequest = null;
			URL m = new URL(urlStr);
			httpRequest = new HttpGet(m.toURI());
			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse response;
			response = (HttpResponse) httpclient.execute(httpRequest);
			HttpEntity entity = response.getEntity();
			// 似乎关键就是这一段
			BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
			InputStream instream = bufHttpEntity.getContent();
			bitmap = BitmapFactory.decodeStream(instream);
			instream.close();
		}
		catch (ClientProtocolException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (URISyntaxException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bitmap;
	}
}